package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.musicroomrank.UserMusicRoomRankBeautyData;
import com.yanqu.road.entity.musicroom.UserMusicRoomBeautyData;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMusicRoomRankBeautyDaoImpl extends TempDataObjectDao<UserMusicRoomRankBeautyData> {
    public static String getUserMusicRoomRankBeautyDataSql = "select * from `t_u_activity_music_room_rank_beauty` where `activityId`=? and `userId`=?";

    public Map<Integer, Map<Integer, UserMusicRoomRankBeautyData>> getUserMusicRoomRankBeautyData(int activityId, long userId) {
        Map<Integer, Map<Integer, UserMusicRoomRankBeautyData>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMusicRoomRankBeautyDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMusicRoomRankBeautyData data = parse(rs);
                Map<Integer, UserMusicRoomRankBeautyData> beautyDataMap = dataMap.get(data.getBeautyId());
                if (beautyDataMap == null) {
                    beautyDataMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getBeautyId(), beautyDataMap);
                }
                beautyDataMap.put(data.getSkinId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserMusicRoomRankBeautyDataSql = "update `t_u_activity_music_room_rank_beauty` set freeTimes=?,itemTimes=?,joinRound=?,showScore=?,showRank=?,showReward=? where activityId=? and userId=? and beautyId=? and skinId=?";
    @Override
    protected boolean updateData(UserMusicRoomRankBeautyData userMusicRoomRankBeautyData) {
        userMusicRoomRankBeautyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getFreeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getItemTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getJoinRound()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomRankBeautyData.getShowScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getShowRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(userMusicRoomRankBeautyData.getShowReward())));

        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomRankBeautyData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getBeautyId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getSkinId()));
        if (getDb().executeNoneQuery(updateUserMusicRoomRankBeautyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserMusicRoomRankBeautyDataSql = "insert into `t_u_activity_music_room_rank_beauty`(activityId,userId,beautyId,skinId,freeTimes,itemTimes,joinRound,showScore,showRank,showReward) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserMusicRoomRankBeautyData userMusicRoomRankBeautyData) {
        userMusicRoomRankBeautyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomRankBeautyData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getBeautyId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getSkinId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getFreeTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getItemTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getJoinRound()));
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomRankBeautyData.getShowScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomRankBeautyData.getShowRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(userMusicRoomRankBeautyData.getShowReward())));
        if (getDb().executeNoneQuery(insertUserMusicRoomRankBeautyDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserMusicRoomRankBeautyData parseData(ResultSet rs) throws SQLException {
        UserMusicRoomRankBeautyData userMusicRoomRankBeautyData = new UserMusicRoomRankBeautyData();
        userMusicRoomRankBeautyData.setActivityId(rs.getInt("activityId"));
        userMusicRoomRankBeautyData.setUserId(rs.getLong("userId"));
        userMusicRoomRankBeautyData.setBeautyId(rs.getInt("beautyId"));
        userMusicRoomRankBeautyData.setSkinId(rs.getInt("skinId"));
        userMusicRoomRankBeautyData.setFreeTimes(rs.getInt("freeTimes"));
        userMusicRoomRankBeautyData.setItemTimes(rs.getInt("itemTimes"));
        userMusicRoomRankBeautyData.setJoinRound(rs.getInt("joinRound"));
        userMusicRoomRankBeautyData.setShowScore(rs.getLong("showScore"));
        userMusicRoomRankBeautyData.setShowRank(rs.getInt("showRank"));
        String showReward = rs.getString("showReward");
        if (!StringUtils.isNullOrEmpty(showReward)) {
            userMusicRoomRankBeautyData.setShowReward(PropertyHelper.parseStringToProperty(showReward));
        }

        return userMusicRoomRankBeautyData;
    }

    public List<UserMusicRoomRankBeautyData> getNeedAutoSendRewardBeautyData(int activityId) {
        String selectSql = "select * from `t_u_activity_music_room_rank_beauty` where `activityId`=? and showRank>0";
        List<UserMusicRoomRankBeautyData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMusicRoomRankBeautyData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

}
